import pandas as pd
from sklearn.preprocessing import LabelEncoder,StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasClassifier
from keras.models import Sequential, load_model
from keras.layers import Dense
from sklearn.metrics import confusion_matrix
# 执行以下步骤进行数据预处理：
# 1.使用pandas加载数据集
# 2.将数据集拆分为输入和输出变量以进行机器学习
# 3.对输入变量进行预处理变换
# 4.将数据汇总以显示其变化
dataset = pd.read_csv('./data.csv')
print(dataset.head(5))
print(dataset.columns.values)
print(dataset.info())
print(dataset.describe())

# 数据清洗
X = dataset.iloc[:,2:32]
print(X.info())
print(type(X))
y = dataset.iloc[:,1]
print(y)
# 将Y encode to 0,1
encoder = LabelEncoder()
y = encoder.fit_transform(y)
print(y[100:110])
# split dataset
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)
# 特征缩放
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# choosing hyper parameters
def classifier(optimizer):
  model = Sequential()
  model.add(Dense(units=16,kernel_initializer='uniform',activation='relu',input_dim=30))
  model.add(Dense(units=8,kernel_initializer='uniform',activation='relu'))
  model.add(Dense(units=6,kernel_initializer='uniform',activation='relu'))
  model.add(Dense(units=1,kernel_initializer='uniform',activation='sigmoid'))
  model.compile(optimizer=optimizer,loss='binary_crossentropy',metrics=['accuracy'])
  return model

model = KerasClassifier(build_fn=classifier)
params = {"batch_size":[1,5],'epochs':[100,120],'optimizer':['adam','rmsprop']}
gridSearch = GridSearchCV(estimator=model,param_grid=params,scoring='accuracy',cv=10)
grid_search = gridSearch.fit(X_train,y_train)
score = grid_search.best_score_
best_params = grid_search.best_params_
print(score)
print(best_params)